package com.app.utils.base.secret;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5 加密类
 * @author sj
 *
 */
public class MD5 {
	
	public static String md5(String plainText) {
		String str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void main(String[] args) throws NoSuchAlgorithmException {
		System.out.println(md5("e5f838b91011f11862268b328e6891df#6633a96fd10738f413768a94391f4151"));
		
		MessageDigest digest=MessageDigest.getInstance("MD5");
    	digest.update("109code=value2name=value3personid=value1202cb962234w4ers2aa".getBytes());
    	byte[] bs=digest.digest();
    	int i; 
    	StringBuffer buf = new StringBuffer(""); 
    	for (int offset = 0; offset < bs.length; offset++) { 
	    	i = bs[offset]; 
	    	if(i<0){
	    		i+= 256;
	    	}
	    	if(i<16){ 
		    	buf.append("0");
	    	}
		    buf.append(Integer.toHexString(i));
    	} 
    	System.out.println("result: " + buf.toString());//32位的加密 

    	System.out.println("result: " + buf.toString().substring(8,24));//16位的加密 

	}
	
}
